srvMap.add('queryAllTree','','front/sh/link!execute?uid=queryAllTree');
srvMap.add('applyQueryJobFuncByUser','','front/sh/link!execute?uid=applyQueryJobFuncByUser');
srvMap.add('applyQueryUserFuncByJob','','front/sh/link!execute?uid=applyQueryUserFuncByJob');
srvMap.add('applyQueryUserJobByFunc','','front/sh/link!execute?uid=applyQueryUserJobByFunc');
srvMap.add('applyModify','','front/sh/link!execute?uid=applyModify');
srvMap.add('applyDelete','','front/sh/link!execute?uid=applyDelete');

var ztree_user, ztree_job, ztree_func;
var isFirst=true;
var userStatus = true;  //默认正常状态， flase 置灰色
var jobStatus = true;
var funcStatus = true;
var userTreeData,jobTreeData,funcTreeData;


$(function() {
	$(".form-submit-add").on("click", addSave);
	$(".form-submit-delete").on("click", deleteSave);
	$("#userSearchBtn").on("click", userSearch);
	$("#jobSearchBtn").on("click", jobSearch);
	$("#funcSearchBtn").on("click", funcSearch);
	$(document).delegate('#funcSearchInput', 'keyup', function(event) {
		var keycode = event.keyCode;
		if (keycode == 13) {
			funcSearch();
		}
	});
	
	$(document).delegate('#jobSearchInput', 'keyup', function(event) {
		var keycode = event.keyCode;
		if (keycode == 13) {
			jobSearch();
		}
	});
	
	$(document).delegate('#userSearchInput', 'keyup', function(event) {
		var keycode = event.keyCode;
		if (keycode == 13) {
			userSearch();
		}
	});
	// 查看模式事件
	bindViewMode();

	// 初始化ztree高度
	initZtreeBox();

	$(window).resize(function(event) {
		// 初始化ztree高度
		initZtreeBox();
	});
	//查询所有树的数据
	initAllTree();
	
});

/**
 * 获取所有树的数据
 * @return {[type]} [description]
 */
function initAllTree(){
	var par = "";
	Util.ajax.postJsonSync(srvMap.get('queryAllTree'),par,function(json,status){
		if(status){
			if (null == json.object ) {
				return;
			}
			if (null != json.object.func ) {
				funcTreeData = json.object.func;
				initFunc(funcTreeData,funcStatus);
			}
			if (null != json.object.user ) {
				userTreeData = json.object.user;
				initUser(userTreeData,userStatus);
			}
			if (null != json.object.job ) {
				jobTreeData = json.object.job;
				initJob(jobTreeData,jobStatus);
			}
			isFirst=false;
			// 隐藏进度条
			$(".loadingSmall").css({
				'background-position': '-1000px -1000px'
			});
		}
		else{
			 alert('初始化列表出错！');
		}
	});
	
//	var url = CONTENT_PATH
//			+ "/com.asiainfo.manage.LinkAction.queryAllTree.invoke";
//	Base.ajax({
//		url : url,
//		success : function(req) {
//			if (null == req ) {
//				return;
//			}
//			if (null != req.func ) {
//				funcTreeData = req.func;
//				initFunc(funcTreeData,funcStatus);
//			}
//			if (null != req.user ) {
//				userTreeData = req.user;
//				initUser(userTreeData,userStatus);
//			}
//			if (null != req.job ) {
//				jobTreeData = req.job;
//				initJob(jobTreeData,jobStatus);
//			}
//			isFirst=false;
//			// 隐藏进度条
//			$(".loadingSmall").css({
//				'background-position': '-1000px -1000px'
//			});
//		}
//	});
}
/**
 * 初始化ztree高度
 * @return {[type]} [description]
 */
function initZtreeBox () {
	var h = $(window).height(),
		bh = $(".form-submit-box").outerHeight(),
		t = $(".ztree").offset().top;
	h = h - bh - t - 31;
	$(".ztree").height(h);
}

/**
 * 初始化用户
 * @return {[type]} [description]
 */
function initUser (data,status) {
			var zNodes = data;
			if(isFirst){
				for (var i = zNodes.length - 1; i >= 0; i--) {
					var z = zNodes[i];
					if(z.type == "user"){
						z.name = '<i class="iconfont icon-yonghu"></i> ' + z.name;
					}
					z.name = z.name + " ["+z.id+"]";
					zNodes[i] = z;
				};
			 }
			
			var setting = {
				view : {
					showIcon: false,
					nameIsHTML: true,
					showTitle: false,
					fontCss : function(treeId, treeNode){
						var o = {};
						if(treeNode.highlight){
							o = {
								color: "red",
								"font-weight": "bold"
							} ;
						}else if(!status){
							o = {
								color: "#bbb",
								"font-weight": "bold"
							};
						}else{
							o ={
								color: "#32b5cb",
								"font-weight": "bold"
							};
						}
						
						return o;
					}
				},
				check: {
					enable: true,
					chkboxType : { "Y" : "s", "N" : "ps" }
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					onCheck: queryJobFuncByUser,
					beforeClick: function(event, treeId, treeNode) {
						return false;
					}
				}
			};

			ztree_user = $.fn.zTree.init($("#userTree"), setting, zNodes);
			if(userQId !=''){
				userCheckedById(userQId);
			}
			dealFolderWithoutChildren(ztree_user);
			ztree_user.expandAll(true);
}

/**
 * 初始化角色
 * flag = true 把整棵树置为灰色
 * @return {[type]} [description]
 */
function initJob (data,status) {
			var zNodes = data;
			if(isFirst){
				for (var i = zNodes.length - 1; i >= 0; i--) {
					var z = zNodes[i];
					if(z.type == "job"){
						z.name = '<i class="iconfont icon-shejishi"></i> ' + z.name;
					}
					z.name = z.name + " ["+z.id+"]";
					zNodes[i] = z;
				};
			 }
			var setting = {
				view : {
					showIcon: false,
					nameIsHTML: true,
					showTitle: false,
					fontCss : function(treeId, treeNode){
						var o = {};
						if(treeNode.highlight){
							o = {
								color: "red",
								"font-weight": "bold"
							};
						}else if(!status){
							o = {
								color: "#bbb",
								"font-weight": "bold"
							};
						}else{
							o ={
								color: "#e89531",
								"font-weight": "bold"
							};
						}
						return o;
					}
				},
				check: {
					enable: true,
					chkboxType : { "Y" : "s", "N" : "ps" }
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					onCheck: queryUserFuncByJob,
					beforeClick: function(event, treeId, treeNode) {
						return false;
					}
				}
			};

			ztree_job = $.fn.zTree.init($("#jobTree"), setting, zNodes);
			if(jobQId !=''){
				jobCheckedById(jobQId);
			}
			dealFolderWithoutChildren(ztree_job);
			ztree_job.expandAll(true);
			
}


/**
 * 初始化功能
 * @return {[type]} [description]
 */
function initFunc (data,status) {
			var zNodes = data;
			if(isFirst){
				for (var i = zNodes.length - 1; i >= 0; i--) {
					var z = zNodes[i];
					if(z.type == "func"){
						z.name = '<i class="iconfont icon-jihuabaobiao"></i> ' + z.name;
					}
					z.name = z.name + " ["+z.id+"]";
					zNodes[i] = z;
				};
			 }
			var setting = {
				view : {
					showIcon: false,
					nameIsHTML: true,
					showTitle: false,
					fontCss : function(treeId, treeNode){
						var o = {};
						if(treeNode.highlight){
							o = {
								color: "red",
								"font-weight": "bold"
							};
						}else if(!status){
							o = {
								color: "#bbb",
								"font-weight": "bold"
							};
						}else{
							o ={
								color: "#32b5cb",
								"font-weight": "bold"
							};
						}
						return o;
					}
				},
				check: {
					enable: true,
					chkboxType : { "Y" : "s", "N" : "ps" }
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					onCheck: queryUserJobByFunc,
					beforeClick: function(event, treeId, treeNode) {
						return false;
					}
				}
			};

			ztree_func = $.fn.zTree.init($("#funcTree"), setting, zNodes);
			if(funcQId !=''){
				funcCheckedById(funcQId);
			}
			dealFolderWithoutChildren(ztree_func);
			var nodes = ztree_func.getNodes();
			if (nodes.length>0) {
				ztree_func.expandNode(nodes[0], true, false, true);
			}
}

/**
 * 初始化功能,并时置灰时，不可选择
 * @return {[type]} [description]
 */
function initFuncDisable (data,status) {
			var zNodes = data;
			if(isFirst){
				for (var i = zNodes.length - 1; i >= 0; i--) {
					var z = zNodes[i];
					if(z.type == "func"){
						z.name = '<i class="iconfont icon-jihuabaobiao"></i> ' + z.name;
					}
					z.name = z.name + " ["+z.id+"]";
					zNodes[i] = z;
				};
			 }
			var setting = {
				view : {
					showIcon: false,
					nameIsHTML: true,
					showTitle: false,
					fontCss : function(treeId, treeNode){
						var o = {};
						if(treeNode.highlight){
							o = {
								color: "red",
								"font-weight": "bold"
							};
						}else if(!status){
							o = {
								color: "#bbb",
								"font-weight": "bold"
							};
						}else{
							o ={
								color: "#32b5cb",
								"font-weight": "bold"
							};
						}
						return o;
					}
				},
				check: {
					enable: false,
					chkboxType : { "Y" : "s", "N" : "ps" }
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					onCheck: queryUserJobByFunc,
					beforeClick: function(event, treeId, treeNode) {
						return false;
					}
				}
			};

			ztree_func = $.fn.zTree.init($("#funcTree"), setting, zNodes);
			if(funcQId !=''){
				funcCheckedById(funcQId);
			}
			dealFolderWithoutChildren(ztree_func);
			var nodes = ztree_func.getNodes();
			if (nodes.length>0) {
				ztree_func.expandNode(nodes[0], true, false, true);
			}
}

/**
 * 
 * @Description : 保存
 * @return void
 * 
 * @author : wucm
 * @date : 2015年8月3日 下午7:42:25
 * 
 */
function addSave() {
	//查看模式不能保存
	if ($("#viewMode").prop("checked")) {
		return;
	}

	//判空
	var user_nodes = ztree_user.getCheckedNodes(true);
	var job_nodes = ztree_job.getCheckedNodes(true);
	var func_nodes = ztree_func.getCheckedNodes(true);
	//拼接参数
	var uId = [],
		uName = [],
		jId = [],
		jName = [],
		fId = [],
		fName = [];
	for (var i = user_nodes.length - 1; i >= 0; i--) {
		var u = user_nodes[i];
		if (u.type == "user") {
			uId.push(u.id);
			uName.push(u.name);
		}
	};
	for (var i = job_nodes.length - 1; i >= 0; i--) {
		var j = job_nodes[i];
		if (j.type == "job") {
			jId.push(j.id);
			jName.push(j.name);
		}
	};
	
	for (var i = func_nodes.length - 1; i >= 0; i--) {
		var f = func_nodes[i];
		if (f.type == "func") {
			fId.push(f.id);
			fName.push(f.name);
		}
	};
	
	var countNull=0;
	if (uId.length == 0){
		countNull++;
	} 
	if(jId.length == 0 ){
		countNull++;
	}
	if( fId.length == 0) {
		countNull++;
	}
	if(countNull>1){
		return ;
	}
	if(fId.length == 0 && func_nodes.length>0){  //选中空文件夹
		alert("空文件夹不能进行赋权！");
		return ;
	}
	
	uId = uId.join(";");
	uName = uName.join(";");
	jId = jId.join(";");
	jName = jName.join(";");	
	fId = fId.join(";");
	fName = fName.join(";");

	var data = {
		"users": uId,
		"jobs": jId,
		"funcs": fId,
	};
	
	var cntStr='确认是否保存以下应用权限：<br/>';
	if(uName!=''){
		if(jName!=''){   //功能可能为空
			cntStr+=uName + "<----->" + jName+"<br/>";
			if(fName!=''){   //三者都不为空，保存user和job，job和func，不保存user和func
				cntStr+=jName + "<----->" + fName+"<br/>";
			}
		}else if(fName!=''){  //只有角色为空
			cntStr+=uName + "<----->" + fName+"<br/>";
		}
	}else if(jName!='' && fName!=''){  //只有用户为空
		cntStr+=jName + "<----->" + fName+"<br/>";
	}
	
	var d = top.dialog({
		title: '保存确认',
		content: cntStr,
		width:'350px',
		okValue: '确定',
		ok: function() {
			this.title('提交中…');

			// 保存提交
			saveSubmit(data);

			return true;
		},
		cancelValue: '取消',
		cancel: function() {}
	});
	d.showModal();
}

/**
 * 保存提交
 * @return {[type]} [description]
 */
function saveSubmit(data) {
	var par = "&users="+data.users+"&jobs="+data.jobs+"&funcs="+data.funcs;
	Util.ajax.postJsonSync(srvMap.get('applyModify'),par,function(json,status){
		if(status){
			if (null == json.object || null == json.object.result) {
				return;
			}
			if (json.object.result) {
				$M.successTip("保存成功");
			} else {
				$M.failureTip("保存失败");
			}
			setTimeout(function() {
				$(".form-tip").html("");
			}, 5000);
		}
	});
	
//	var url = CONTENT_PATH + "/com.asiainfo.manage.LinkAction.applyModify.invoke";
//	Base.ajax({
//		url: url,
//		data: data,
//		success: function(req) {
//			if (null == req || null == req.result) {
//				return;
//			}
//			if (req.result) {
//				$M.successTip("保存成功");
//			} else {
//				$M.failureTip("保存失败");
//			}
//			setTimeout(function() {
//				$(".form-tip").html("");
//			}, 5000);
//		}
//	});
}

/**
 * 查看模式事件
 * @return {[type]} [description]
 */
function bindViewMode () {
	var _o = $("#viewMode");
	_o.change(function(event) {
		var flag = _o.prop("checked");
		var _os = $(".form-submit");
		clearAllLight();		
		if(flag){
			_os.prop("disabled", true);
			$(".form-tip").val('');
			ztree_user.checkAllNodes(false);
			ztree_job.checkAllNodes(false);
			ztree_func.checkAllNodes(false);
		}else{
			_os.prop("disabled", false);
		}
	});
}

/**
 * 用户搜索
 * @return {[type]} [description]
 */
function userSearch() {
	var s = $("#userSearchInput").val();
	s = $.trim(s);

	var nodes = ztree_user.getNodes();
	nodes = ztree_user.transformToArray(nodes);

	for (var i = nodes.length - 1; i >= 0; i--) {
		var node = nodes[i];
		var id = node.id;
		var name = node.name;
		var type = node.type;

		// 先显示节点，再进行搜索匹配
		ztree_user.showNode(node);

		if (s.length > 0 && (id.indexOf(s) != -1 || name.indexOf(s) != -1) && "user" == type) {
			node.highlight = true;
			ztree_user.updateNode(node);
		}else if(s.length > 0 && "user" == type){
 			ztree_user.hideNode(node);
			ztree_user.updateNode(node);
		}else if("user" == type){   //没有匹配字串的时候都常规显示
			node.highlight = false;
			ztree_user.showNode(node);
 			ztree_user.updateNode(node);
		}
	};
}
/**
 * 角色搜索
 * @return {[type]} [description]
 */
function jobSearch() {
	var s = $("#jobSearchInput").val();
	s = $.trim(s);

	var nodes = ztree_job.getNodes();
	nodes = ztree_job.transformToArray(nodes);

	for (var i = nodes.length - 1; i >= 0; i--) {
		var node = nodes[i];
		var id = node.id;
		var name = node.name;
		var type = node.type;

		// 先显示节点，再进行搜索匹配
		ztree_job.showNode(node);

		if (s.length > 0 && (id.indexOf(s) != -1 || name.indexOf(s) != -1) && "job" == type) {
			node.highlight = true;
			ztree_job.updateNode(node);
		}else if(s.length > 0 && "job" == type){
 			ztree_job.hideNode(node);
			ztree_job.updateNode(node);
		}else if("job" == type){   //没有匹配字串的时候都常规显示
			node.highlight = false;
			ztree_job.showNode(node);
 			ztree_job.updateNode(node);
		}
	};
}

/**
 * 功能搜索
 * @return {[type]} [description]
 */
function funcSearch() {
	var s = $("#funcSearchInput").val();
	s = $.trim(s);

	var nodes = ztree_func.getNodes();
	nodes = ztree_func.transformToArray(nodes);

	for (var i = nodes.length - 1; i >= 0; i--) {
		var node = nodes[i];
		var id = node.id;
		var name = node.name;
		var type = node.type;

		// 先显示节点，再进行搜索匹配
		ztree_func.showNode(node);
		
		if (s.length > 0 && (id.indexOf(s) != -1 || name.indexOf(s) != -1) && "func" == type) {
			node.highlight = true;
			ztree_func.updateNode(node);
		}else if(s.length > 0 && "func" == type){
 			ztree_func.hideNode(node);
			ztree_func.updateNode(node);
		}else if("func" == type){   //没有匹配字串的时候都常规显示
			node.highlight = false;
			ztree_func.showNode(node);
 			ztree_func.updateNode(node);
		}
	};
}

/**
 * 根据用户查询角色功能
 * @return {[type]} [description]
 */
function queryJobFuncByUser(event, treeId, treeNode) {
	//如果是查看模式，则只能单选
	if ($("#viewMode").prop("checked")) {
		var nodes = ztree_user.getNodes();
		nodes = ztree_user.transformToArray(nodes);
		for (var i = nodes.length - 1; i >= 0; i--) {
			var node = nodes[i];
			if (node.tId != treeNode.tId) {
				ztree_user.checkNode(node, false, false);
				node.highlight = false;
				ztree_user.updateNode(node);
			}
		};

		if(treeNode.checked){
			treeNode.highlight = true;
			ztree_user.updateNode(treeNode);
		}else{
			treeNode.highlight = false;
			ztree_user.updateNode(treeNode);
		}
	} else {
		chooseUserTreeStatus(treeNode);
		return;
	}

	//先清空ztree_job和ztree_func状态
	clearStatusForJob();
	clearStatusForFunc();

	var user_nodes = ztree_user.getCheckedNodes(true);
	if (user_nodes.length != 1) {
		return;
	}

	//参数
	var node = user_nodes[0];
	var userId = node.id;
	var data = {
		userId: userId
	};
	
	var par = "&selUserId="+userId;
	Util.ajax.postJsonSync(srvMap.get('applyQueryJobFuncByUser'),par,function(json,status){
		if(status){
			if (null == json.object) {
				return;
			}
			//添加ztree_job状态
			var data = json.object.job;
			for (var i = data.length - 1; i >= 0; i--) {
				var id = data[i];
				var node = ztree_job.getNodeByParam("id", id, null);
				if (null == node) {
					continue;
				}
				node.highlight = true;
				ztree_job.checkNode(node, true, true);
				ztree_job.updateNode(node);
			};			
			//添加ztree_func状态
			var data = json.object.func;
			for (var i = data.length - 1; i >= 0; i--) {
				var id = data[i];
				var node = ztree_func.getNodeByParam("id", id, null);
				if (null == node) {
					continue;
				}
				node.highlight = true;
				ztree_func.checkNode(node, true, true);
				ztree_func.updateNode(node);
			};
		}
		else{
			 alert('根据用户查询角色功能失败！');
		}
	});
	
//	var url = CONTENT_PATH + "/com.asiainfo.manage.LinkAction.applyQueryJobFuncByUser.invoke";
//	Base.ajax({
//		url: url,
//		data: data,
//		success: function(req) {
//			if (null == req || null == req.result) {
//				return;
//			}
//
//			//添加ztree_job状态
//			var data = req.result.job;
//			for (var i = data.length - 1; i >= 0; i--) {
//				var id = data[i];
//				var node = ztree_job.getNodeByParam("id", id, null);
//				if (null == node) {
//					continue;
//				}
//				node.highlight = true;
//				ztree_job.checkNode(node, true, true);
//				ztree_job.updateNode(node);
//			};			
//			//添加ztree_func状态
//			var data = req.result.func;
//			for (var i = data.length - 1; i >= 0; i--) {
//				var id = data[i];
//				var node = ztree_func.getNodeByParam("id", id, null);
//				if (null == node) {
//					continue;
//				}
//				node.highlight = true;
//				ztree_func.checkNode(node, true, true);
//				ztree_func.updateNode(node);
//			};
//		}
//	});
}

/**
 * 根据角色查询用户功能
 * @return {[type]} [description]
 */
function queryUserFuncByJob(event, treeId, treeNode) {
	//如果是查看模式，则只能单选
	if ($("#viewMode").prop("checked")) {
		var nodes = ztree_job.getNodes();
		nodes = ztree_job.transformToArray(nodes);
		for (var i = nodes.length - 1; i >= 0; i--) {
			var node = nodes[i];
			if (node.tId != treeNode.tId) {
				ztree_job.checkNode(node, false, false);
				node.highlight = false;
				ztree_job.updateNode(node);
			}
		};

		if(treeNode.checked){
			treeNode.highlight = true;
			ztree_job.updateNode(treeNode);
		}else{
			treeNode.highlight = false;
			ztree_job.updateNode(treeNode);
		}
	} else {
		chooseJobTreeStatus(treeNode);
		return;
	}

	//先清空ztree_user和ztree_func状态
	clearStatusForUser();
	clearStatusForFunc();

	var job_nodes = ztree_job.getCheckedNodes(true);
	if (job_nodes.length != 1) {
		return;
	}

	//参数
	var node = job_nodes[0];
	var jobId = node.id;
	var data = {
		jobId: jobId
	};
	var par = "&jobId="+jobId;
	Util.ajax.postJsonSync(srvMap.get('applyQueryUserFuncByJob'),par,function(json,status){
		if(status){
			if (null == json.object ) {
				return;
			}

			//添加ztree_user状态
			var data = json.object.user;
			for (var i = data.length - 1; i >= 0; i--) {
				var id = data[i];
				var node = ztree_user.getNodeByParam("id", id, null);
				if (null == node) {
					continue;
				}
				node.highlight = true;
				ztree_user.checkNode(node, true, true);
				ztree_user.updateNode(node);
			};			
			//添加ztree_func状态
			var data = json.object.func;
			for (var i = data.length - 1; i >= 0; i--) {
				var id = data[i];
				var node = ztree_func.getNodeByParam("id", id, null);
				if (null == node) {
					continue;
				}
				node.highlight = true;
				ztree_func.checkNode(node, true, true);
				ztree_func.updateNode(node);
			};
		}
		else{
			 alert('根据角色查询用户功能失败！');
		}
	});
//	var url = CONTENT_PATH + "/com.asiainfo.manage.LinkAction.applyQueryUserFuncByJob.invoke";
//	Base.ajax({
//		url: url,
//		data: data,
//		success: function(req) {
//			if (null == req || null == req.result) {
//				return;
//			}
//
//			//添加ztree_user状态
//			var data = req.result.user;
//			for (var i = data.length - 1; i >= 0; i--) {
//				var id = data[i];
//				var node = ztree_user.getNodeByParam("id", id, null);
//				if (null == node) {
//					continue;
//				}
//				node.highlight = true;
//				ztree_user.checkNode(node, true, true);
//				ztree_user.updateNode(node);
//			};			
//			//添加ztree_func状态
//			var data = req.result.func;
//			for (var i = data.length - 1; i >= 0; i--) {
//				var id = data[i];
//				var node = ztree_func.getNodeByParam("id", id, null);
//				if (null == node) {
//					continue;
//				}
//				node.highlight = true;
//				ztree_func.checkNode(node, true, true);
//				ztree_func.updateNode(node);
//			};
//		}
//	});
}

/**
 * 
* @Description :清空ztree_user状态
* @author  xueyf3 
* @date 创建时间：2015年8月24日 下午3:07:38 
* @version 1.0
 */
function clearStatusForUser(){
	ztree_user.checkAllNodes(false);
	var hNodes = ztree_user.getNodes();
	hNodes = ztree_user.transformToArray(hNodes);
	for (var i = hNodes.length - 1; i >= 0; i--) {
		var node = hNodes[i];
		node.highlight = false;
		ztree_user.updateNode(node);
	};
}

/**
 * 
* @Description :清空ztree_user状态
* @author  xueyf3 
* @date 创建时间：2015年8月24日 下午3:07:38 
* @version 1.0
 */
function clearStatusForJob(){
	ztree_job.checkAllNodes(false);
	var hNodes = ztree_job.getNodes();
	hNodes = ztree_job.transformToArray(hNodes);
	for (var i = hNodes.length - 1; i >= 0; i--) {
		var node = hNodes[i];
		node.highlight = false;
		ztree_job.updateNode(node);
	};
}

/**
 * 
* @Description :清空ztree_func状态
* @author  xueyf3 
* @date 创建时间：2015年8月24日 下午3:08:52 
* @version 1.0
 */
function clearStatusForFunc(){
	ztree_func.checkAllNodes(false);
	var hNodes = ztree_func.getNodes();
	hNodes = ztree_func.transformToArray(hNodes);
	for (var i = hNodes.length - 1; i >= 0; i--) {
		var node = hNodes[i];
		node.highlight = false;
		ztree_func.updateNode(node);
	};
}

/**
 * 根据功能更查询用户角色
 * @return {[type]} [description]
 */
function queryUserJobByFunc(event, treeId, treeNode) {
	//如果是查看模式，则只能单选
	if ($("#viewMode").prop("checked")) {
		var nodes = ztree_func.getNodes();
		nodes = ztree_func.transformToArray(nodes);
		for (var i = nodes.length - 1; i >= 0; i--) {
			var node = nodes[i];
			if (node.tId != treeNode.tId) {
				ztree_func.checkNode(node, false, false);
				node.highlight = false;
				ztree_func.updateNode(node);
			}
		};

		if(treeNode.checked){
			treeNode.highlight = true;
			ztree_func.updateNode(treeNode);
		}else{
			treeNode.highlight = false;
			ztree_func.updateNode(treeNode);
		}
	} else {
		chooseFuncTreeStatus(treeNode);
		return;
	}

	//先清空ztree_job和ztree_user状态
	
	clearStatusForJob();
	clearStatusForUser();

	var func_nodes = ztree_func.getCheckedNodes(true);
	if (func_nodes.length != 1) {
		return;
	}

	//参数
	var node = func_nodes[0];
	var funcId = node.id;
	var data = {
		funcId: funcId
	};
	var par = "&funcId="+funcId;
	Util.ajax.postJsonSync(srvMap.get('applyQueryUserJobByFunc'),par,function(json,status){
		if(status){
			if (null == json.object) {
				return;
			}
			//添加ztree_job状态
			var data = json.object.job;
			for (var i = data.length - 1; i >= 0; i--) {
				var id = data[i];
				var node = ztree_job.getNodeByParam("id", id, null);
				if (null == node) {
					continue;
				}
				node.highlight = true;
				ztree_job.checkNode(node, true, true);
				ztree_job.updateNode(node);
			};			
			//添加ztree_user状态
			var data = json.object.user;
			for (var i = data.length - 1; i >= 0; i--) {
				var id = data[i];
				var node = ztree_user.getNodeByParam("id", id, null);
				if (null == node) {
					continue;
				}
				node.highlight = true;
				ztree_user.checkNode(node, true, true);
				ztree_user.updateNode(node);
			}
		}
		else{
			 alert('根据功能更查询用户角色失败！');
		}
	});
//	var url = CONTENT_PATH + "/com.asiainfo.manage.LinkAction.applyQueryUserJobByFunc.invoke";
//	Base.ajax({
//		url: url,
//		data: data,
//		success: function(req) {
//			if (null == req || null == req.result) {
//				return;
//			}
//
//			//添加ztree_job状态
//			var data = req.result.job;
//			for (var i = data.length - 1; i >= 0; i--) {
//				var id = data[i];
//				var node = ztree_job.getNodeByParam("id", id, null);
//				if (null == node) {
//					continue;
//				}
//				node.highlight = true;
//				ztree_job.checkNode(node, true, true);
//				ztree_job.updateNode(node);
//			};			
//			//添加ztree_user状态
//			var data = req.result.user;
//			for (var i = data.length - 1; i >= 0; i--) {
//				var id = data[i];
//				var node = ztree_user.getNodeByParam("id", id, null);
//				if (null == node) {
//					continue;
//				}
//				node.highlight = true;
//				ztree_user.checkNode(node, true, true);
//				ztree_user.updateNode(node);
//			};
//		}
//	});
}
/**
 * 
* @Description :去掉红色高亮显示
* @author  xueyf3 
* @date 创建时间：2015年8月29日 下午9:24:10 
* @version 1.0
 */
function clearAllLight(){
	userStatus=true;
	jobStatus=true;
	funcStatus=true;
	initUser(userTreeData,userStatus);
	initJob(jobTreeData,jobStatus);
	initFunc(funcTreeData,funcStatus);
}
/**
 * 
* @Description :删除捆绑
* @author  xueyf3 
* @date 创建时间：2015年8月27日 下午3:14:46 
* @version 1.0
 */
function deleteSave() {
	//查看模式不能保存
	if ($("#viewMode").prop("checked")) {
		return;
	}

	//判空
	var user_nodes = ztree_user.getCheckedNodes(true);
	var job_nodes = ztree_job.getCheckedNodes(true);
	var func_nodes = ztree_func.getCheckedNodes(true);
	var countNull=0;
	if (user_nodes.length == 0){
		countNull++;
	} 
	if(job_nodes.length == 0 ){
		countNull++;
	}
	if( func_nodes.length == 0) {
		countNull++;
	}
	if(countNull>1){
		return ;
	}
	//拼接参数
	var uId = [],
		uName = [],
		jId = [],
		jName = [],
		fId = [],
		fName = [];
	for (var i = user_nodes.length - 1; i >= 0; i--) {
		var u = user_nodes[i];
		if (u.type == "user") {
			uId.push(u.id);
			uName.push(u.name);
		}
	};
	for (var i = job_nodes.length - 1; i >= 0; i--) {
		var j = job_nodes[i];
		if (j.type == "job") {
			jId.push(j.id);
			jName.push(j.name);
		}
	};
	
	for (var i = func_nodes.length - 1; i >= 0; i--) {
		var f = func_nodes[i];
		if (f.type == "func") {
			fId.push(f.id);
			fName.push(f.name);
		}
	};
	uId = uId.join(";");
	uName = uName.join(";");
	jId = jId.join(";");
	jName = jName.join(";");	
	fId = fId.join(";");
	fName = fName.join(";");

	var data = {
		"users": uId,
		"jobs": jId,
		"funcs": fId,
	};
	
	var cntStr='确认是否删除以下应用权限：<br/>';
	if(uName!=''){
		if(jName!=''){   //功能可能为空
			cntStr+=uName + "<----->" + jName+"<br/>";
			if(fName!=''){   //三者都不为空，删除user和job，job和func、user和func
				cntStr+=uName + "<----->" + fName+"<br/>";
				cntStr+=jName + "<----->" + fName+"<br/>";
			}
		}else if(fName!=''){  //只有角色为空
			cntStr+=uName + "<----->" + fName+"<br/>";
		}
	}else if(jName!='' && fName!=''){  //只有用户为空
		cntStr+=jName + "<----->" + fName+"<br/>";
	}
	
	var d = top.dialog({
		title: '删除确认',
		content: cntStr,
		width:'350px',
		okValue: '确定',
		ok: function() {
			this.title('提交中…');

			// 保存提交
			deleteSubmit(data);

			return true;
		},
		cancelValue: '取消',
		cancel: function() {}
	});
	d.showModal();
}

/**
 * 数据权限删除-保存提交
 * @return {[type]} [description]
 */
function deleteSubmit(data) {
	var par = "&users="+data.users+"&jobs="+data.jobs+"&funcs="+data.funcs;
	Util.ajax.postJsonSync(srvMap.get('applyDelete'),par,function(json,status){
		if(status){
			if (null == json.object || null == json.object.result) {
				return;
			}

			if (json.object.result) {
				$M.successTip("删除成功!");
				clearAllLight();
			} else {
				$M.failureTip("删除失败!");
			}
			setTimeout(function() {
				$(".form-tip").html("");
			}, 5000);
		}
	});
	
	
//	var url = CONTENT_PATH + "/com.asiainfo.manage.LinkAction.applyDelete.invoke";
//	Base.ajax({
//		url: url,
//		data: data,
//		success: function(req) {
//			if (null == req || null == req.result) {
//				return;
//			}
//
//			if (req.result) {
//				$M.successTip("删除成功!");
//				clearAllLight();
//			} else {
//				$M.failureTip("删除失败!");
//			}
//			setTimeout(function() {
//				$(".form-tip").html("");
//			}, 5000);
//		}
//	});
}

/**
 * 根据需要赋权的用户编码，初始化用户树
 * @return {[type]} [description]
 */
function  userCheckedById(userId){
	var nodes = ztree_user.getNodes();
	nodes = ztree_user.transformToArray(nodes);
	for (var i = nodes.length - 1; i >= 0; i--) {
		var node = nodes[i];
		var id = node.id;
		var name = node.name;
		var type = node.type;
		if (id == userId && "user" == type) {
			node.checked = true;
			ztree_user.updateNode(node);
		}else if("user" == type){
 			ztree_user.removeNode(node);
		}
	};
}

/**
 * 根据需要赋权的角色编码，初始化角色树
 * @return {[type]} [description]
 */
function  jobCheckedById(jobId){
	var nodes = ztree_job.getNodes();
	nodes = ztree_job.transformToArray(nodes);
	for (var i = nodes.length - 1; i >= 0; i--) {
		var node = nodes[i];
		var id = node.id;
		var name = node.name;
		var type = node.type;
		if (id == jobId && "job" == type) {
			node.checked = true;
			ztree_job.updateNode(node);
		}else if("job" == type){
 			ztree_job.removeNode(node);
		}
	};
}

/**
 * 根据需要赋权的功能编码，初始化功能树
 * @return {[type]} [description]
 */
function  funcCheckedById(funcId){
	var nodes = ztree_func.getNodes();
	nodes = ztree_func.transformToArray(nodes);
	for (var i = nodes.length - 1; i >= 0; i--) {
		var node = nodes[i];
		var id = node.id;
		var name = node.name;
		var type = node.type;
		if (id == funcId && "func" == type) {
			node.checked = true;
			ztree_func.updateNode(node);
		}else if("func" == type){
 			ztree_func.removeNode(node);
		}
	};
}
/**
 * 非查看模式下，点击用户树事件
 * @return {[type]} [description]
 */
function chooseUserTreeStatus(treeNode){
	var tId=treeNode.tId;
	if(!userStatus){
		userStatus=true;
		initUser(userTreeData,userStatus);
		var node = ztree_user.getNodeByTId(tId);
		setCheckForNode(node,ztree_user);
	}
	var user_nodes = ztree_user.getCheckedNodes(true);
	var func_nodes = ztree_func.getCheckedNodes(true);
	var job_nodes = ztree_job.getCheckedNodes(true);
	
	if(job_nodes.length>0){  //给角色赋权状态下
		if(user_nodes.length>0 && funcStatus){  //第一次点击用户树节点时,置灰
			funcStatus=false;
			initFunc(funcTreeData,funcStatus);
		}else if(user_nodes.length==0 && !funcStatus){  //恢复正常
		    funcStatus=true;
			initFunc(funcTreeData,funcStatus);
		}
	}else if(func_nodes.length>0){ //给功能赋权状态，角色树重新生成并置灰
		if(user_nodes.length>0 && jobStatus){  //第一次点击用户树节点时,置灰
			jobStatus=false;
			initJob(jobTreeData,jobStatus);
		}else if(user_nodes.length==0 && !jobStatus){  //恢复正常
		    jobStatus=true;
			initJob(jobTreeData,jobStatus);
		}
	}
	
	//当角色列表中只选中“管理员”时，功能列表全部置灰，并且不可选-20161012
	if(user_nodes.length==0 && job_nodes.length==1 && job_nodes[0].id==ADMIN_JOB_ID){
		funcStatus=false;
		initFuncDisable(funcTreeData,funcStatus);	
	}
}

/**
 * 非查看模式下，点击角色树事件
 * @return {[type]} [description]
 */
function chooseJobTreeStatus(treeNode){
	var tId=treeNode.tId;
	if(!jobStatus){
		jobStatus=true;
		initJob(jobTreeData,jobStatus);
		var node = ztree_job.getNodeByTId(tId);
		setCheckForNode(node,ztree_job);
	}
	var user_nodes = ztree_user.getCheckedNodes(true);
	var func_nodes = ztree_func.getCheckedNodes(true);
	var job_nodes = ztree_job.getCheckedNodes(true);
	
	
	//当角色列表中只选中“管理员”时，功能列表全部置灰，并且不可选-20161012
	if(user_nodes.length==0 && job_nodes.length==1 && job_nodes[0].id==ADMIN_JOB_ID){
		funcStatus=false;
		initFuncDisable(funcTreeData,funcStatus);	
	}
	//当角色列表中取消只选中“管理员”时，恢复功能列表-20161012
	if(user_nodes.length==0 && job_nodes.length!=1){
		funcStatus=true;
		initFunc(funcTreeData,funcStatus);	
	}
	
	
	if(user_nodes.length>0){   //不允许自身树再响应点击事件
		if(job_nodes.length>0 && funcStatus){  //第一次点击角色树节点时,置灰功能
			funcStatus=false;
			initFunc(funcTreeData,funcStatus);
		}else if(job_nodes.length==0 && !funcStatus){  //恢复正常
		    funcStatus=true;
			initFunc(funcTreeData,funcStatus);
		}
	}else if(func_nodes.length>0){ //给功能赋权状态
		if(job_nodes.length>0 && userStatus){  //第一次点击角色树节点时,置灰用户树
			userStatus=false;
			initUser(userTreeData,userStatus);
		}else if(job_nodes.length==0 && !userStatus){  //恢复正常
		    userStatus=true;
			initUser(userTreeData,userStatus);
		}
	}
}

/**
 * 非查看模式下，点击功能树事件
 * @return {[type]} [description]
 */
function chooseFuncTreeStatus(treeNode){
	var tId=treeNode.tId;
	if(!funcStatus){
		funcStatus=true;
		initFunc(funcTreeData,funcStatus);
		var node = ztree_func.getNodeByTId(tId);
		setCheckForNode(node,ztree_func);
	}
	var user_nodes = ztree_user.getCheckedNodes(true);
	var func_nodes = ztree_func.getCheckedNodes(true);
	var job_nodes = ztree_job.getCheckedNodes(true);
	if(user_nodes.length>0){   //不允许自身树再响应点击事件
		if(func_nodes.length>0 && jobStatus){  //第一次点击功能树节点时,置灰角色
			jobStatus=false;
			initJob(jobTreeData,jobStatus);
		}else if(func_nodes.length==0 && !jobStatus){  //恢复正常
		    jobStatus=true;
			initJob(jobTreeData,jobStatus);
		}
	}else if(job_nodes.length>0){ //给功能赋权状态
		if(func_nodes.length>0 && userStatus){  //第一次点击角色树节点时,置灰用户树
			userStatus=false;
			initUser(userTreeData,userStatus);
		}else if(func_nodes.length==0 && !userStatus){  //恢复正常
		    userStatus=true;
			initUser(userTreeData,userStatus);
		}
	}
}
	/**
	*去掉树上的空节点
	*
	**/
function dealFolderWithoutChildren(treeObj){
	var nodes = treeObj.getNodes();
	nodes = treeObj.transformToArray(nodes);
	for (var i = nodes.length - 1; i >= 0; i--) {
		var node = nodes[i];
		var isParent = node.isParent;
		if(isParent && !node.children){
			treeObj.removeNode(node);
		}
	};
}
/**
	*根据勾选节点，以及节点的所有子节点
	*
	**/
function setCheckForNode(node,treeObj){
		if(!node.isParent){
			node.checked = true ;
			treeObj.updateNode(node);
		}else if(node.isParent && node.children){
			node.checked = true ;
			treeObj.updateNode(node);
			var children=node.children;
			for(var i=0;i<children.length;i++){
				var cnode=children[i];
				setCheckForNode(cnode,treeObj);
			}
		}
}